package com.jasonzqshen.ifm.wizards;

import java.net.URI;

import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.wizard.IWizardContainer;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;

import com.jasonzqshen.ifm.Plugin;
import com.jasonzqshen.ifm.PluginConfiguration;
import com.jasonzqshen.ifm.project.ProjectCreator;
import com.jasonzqshen.ifm.utils.SystemLog;

/**
 * This is a feature modeling new wizard. Its role is to create a new feature
 * modeling project and an addition project file with the extension "fm".
 */
//@SuppressWarnings("restriction")
public class NewProjectWizard extends BasicNewResourceWizard{

    private NewProjectWizardPage page;

    /**
     * Constructor for NewProjectWizard
     */
    public NewProjectWizard() {
        IDialogSettings workbenchSettings = Plugin.getDefault()
                .getDialogSettings();
        IDialogSettings section = workbenchSettings
                .getSection(PluginConfiguration.NEW_PROJECT_WIZARD_ID);
        if (section == null) {
            section = workbenchSettings
                    .addNewSection(PluginConfiguration.NEW_PROJECT_WIZARD_ID);
        }
        setDialogSettings(section);
        setWindowTitle("New Ifm feature modeling project.");

        SystemLog.logInfo("NewProjectWizard constructing complete.");
    }

    /**
     * Adding the page to the wizard
     */
    @Override
    public void addPages() {
        page = new NewProjectWizardPage();
        addPage(page);

        SystemLog.logInfo("Adding new project wizard page complete.");
    }

    /**
     * This method is called when 'Finish' button is pressed in the wizard. We
     * will create an operation and run it using wizard as execution context.
     */
    @Override
    public boolean performFinish() {
        SystemLog.logInfo("User clicks 'Finish' button on new project wizard");

        final String projectName = page.getProjectName();
        URI uri = null;
        if (!page.useDefaults()) {
            uri = page.getLocationURI();
            SystemLog.logInfo(String.format("Project Name: %s; URI: %s.",
                    projectName, uri.toString()));
        } else {
            SystemLog.logInfo(String.format("Project Name: %s; Default URI.",
                    projectName));
        }
        
        final Shell shell = getShell();
        IWizardContainer container = getContainer();
        // create new project
        ProjectCreator.createFeatureModelProject(projectName, uri, shell, container);

        return true;
    }
}
